import time, random
from machine import Pin

def run(ed, k_u, k_d, k_l, k_a, k_b): 
    score = [0, 0]  # 初始化玩家得分和电脑得分
    while True:
        ed.fill(0x0000)  # 清屏
        ed.text("A键开始，B键返回", 10, 50, color=0xFFFF)  # 提示信息，居中显示
        # 等待用户选择开始或返回
        start = False
        while not start:
            if k_a.value() == 0:  # 按下 A 键开始
                time.sleep(0.2)   # 防抖
                start = True
            elif k_b.value() == 0:  # 按下 B 键返回
                time.sleep(0.2)   # 防抖
                return
        ed.fill(0x0000)  # 清屏
        ed.rect(0, 0, 160, 128, 0xFFFF)  # 绘制边框
        ed.text("上键：石头", 20, 20, color=0x00FF)  # 玩家操作提示
        ed.text("下键：剪刀", 20, 40, color=0x00FF)
        ed.text("左键：布", 20, 60, color=0x00FF)
        ed.show()
        time.sleep(0.5)  # 显示提示信息
        # 玩家选择
        player_choice = None
        while not player_choice:
            if k_u.value() == 0:
                player_choice = "石头"
            elif k_d.value() == 0:
                player_choice = "剪刀"
            elif k_l.value() == 0:  # 使用 k_l 检测左键
                player_choice = "布"
            time.sleep(0.1)  # 防抖
        # 电脑随机选择
        choices = ["石头", "剪刀", "布"]
        computer_choice = random.choice(choices)
        # 显示对决结果
        ed.fill(0x0000)  # 清屏
        ed.rect(0, 0, 160, 128, 0xFFFF)  # 绘制边框
        ed.text(player_choice, 30, 30, color=0xFFFF)
        ed.text("VS", 70, 40, color=0xF800)  # 显示 VS
        ed.text(computer_choice, 100, 30, color=0xFFFF)
        ed.show()
        time.sleep(1)  # 显示结果一段时间
        # 判断胜负
        result = ""
        if player_choice == computer_choice:
            result = "平局！"
        elif (player_choice == "石头" and computer_choice == "剪刀") or \
             (player_choice == "剪刀" and computer_choice == "布") or \
             (player_choice == "布" and computer_choice == "石头"):
            result = "你赢了！"
            score[0] += 1
        else:
            result = "你输了！"
            score[1] += 1
        # 显示结果
        ed.fill(0x0000)  # 清屏
        ed.rect(0, 0, 160, 128, 0xFFFF)  # 绘制边框
        ed.text("对决结果:", 5, 10, color=0xFFFF)  # 显示对决结果
        ed.text(result, 40, 40, color=0x00FF)  # 显示结果
        ed.text("你的得分: {}".format(score[0]), 5, 60, color=0xFFFF)  # 显示玩家得分
        ed.text("电脑得分: {}".format(score[1]), 5, 80, color=0xFFFF)  # 显示电脑得分
        ed.show()
        time.sleep(2)  # 显示结果一段时间